<?php

namespace Home\Controller;

use Home\Controller\BaseController;

class UsersController extends BaseController {

    public function getUserInfo() {
        $userInfo = M()->field("u.nick_name,u.face,u.real_name,u.sex,u.phone,u.company,u.position,u.channel,u.company_address,u.fax")
                        ->table("ot_users as u ")
                        ->where(['u.user_id' => $this->user_id])->find();

        if ($userInfo) {
            $userInfo['face'] = 'http://' . $_SERVER['HTTP_HOST'] . __ROOT__ . $userInfo['face'];
        }
        $this->success('获取成功', $userInfo);
    }

    public function setUserInfo() {
        $face = I('face');
        $nick_name = I('nick_name');
        $real_name = I('real_name');

        $sex = I('sex', -1, 'intval');

        $company = I('company');

        $company_address = I('company_address');

        $channel = I('channel');
        $position = I('position');

        $fax = I('fax');

        if ($face) {
            if (is_base_64($face)) {
                $code = md5(time() + mt_rand(1, 9999));
                $s = file_put_contents('./Uploads/Picture/' . $code . '.png', base64_decode($face)); //返回的是字节数

                $data['face'] = '/Uploads/Picture/' . $code . '.png';
            }
        }

        if ($real_name) {
            $data['real_name'] = $real_name;
        }

        if ($nick_name) {
            $data['nick_name'] = $nick_name;
        }

        if ($sex != -1) {
            $data['sex'] = $sex;
        }

        if ($company) {
            $data['company'] = $company;
        }

        if ($company_address) {
            $data['company_address'] = $company_address;
        }

        if ($channel) {
            $data['channel'] = $channel;
        }
        if ($position) {
            $data['position'] = $position;
        }
        if ($fax) {
            $data['fax'] = $fax;
        }

        if ($data) {
            $data['is_perfect'] = 1;
            M('Users')->where(['user_id' => $this->user_id])->save($data);
        }

        $this->success('修改成功');
    }

    public function setOutTime() {
        $log_login_id = M('LogLogin')->where(['user_id' => $this->user_id, 'end_time' => array('EXP','IS NULL')])->order('login_log_id desc')->getField('login_log_id');

        if ($log_login_id) {
            $end_time = date('Y-m-d H:i:s',time());
            M('LogLogin')->where(['login_log_id' => $log_login_id])->save(['end_time' => $end_time]);
        }
        $this->success('执行成功');
    }

    public function setReadGoodsTime() {
        $goods_id = I('goods_id', 0, 'intval');

        if ($goods_id == 0) {
            $this->error('请传入商品ID');
        }
        $type = I('type', 0, 'intval');
        $data['user_id'] = $this->user_id;
        $data['goods_id'] = $goods_id;

        if ($type == 0) {
            M('LogGoods')->add($data);
        } else {
            $data['end_time'] =array('EXP','IS NULL');;
            $end_time = date('Y-m-d H:i:s',time());

            $log_goods_id = M('LogGoods')->where($data)->order('log_goods_id desc')->getField('log_goods_id');

            M('LogGoods')->where(['log_goods_id' => $log_goods_id])->save(['end_time' => $end_time]);
        }

        $this->success('执行成功');
    }

}
